<template>
  <div class="container">
    <div class="page-header">
      <h1>🔐 用户登录</h1>
    </div>

    <div class="form-container">
      <a-form 
        :model="loginForm" 
        :rules="rules" 
        ref="loginFormRef"
        layout="vertical"
        size="large"
      >
        <a-form-item label="用户名" field="username">
          <a-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            allow-clear
          >
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item label="密码" field="password">
          <a-input-password
            v-model="loginForm.password"
            placeholder="请输入密码"
            allow-clear
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
          <div style="display: flex; justify-content: space-between; align-items: center; width: 100%;">
            <a-checkbox v-model="loginForm.remember">记住我</a-checkbox>
            <a-link>忘记密码？</a-link>
          </div>
        </a-form-item>

        <a-form-item>
          <a-button 
            type="primary" 
            long
            size="large"
            :loading="loading"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </a-button>
        </a-form-item>
      </a-form>

      <div class="footer-links">
        <p style="text-align: center; color: #666; margin: 20px 0;">
          还没有账号？
          <a-link @click="$router.push('/register')">
            立即注册
          </a-link>
        </p>
      </div>

      <div class="social-login">
        <a-divider>或</a-divider>
        <div style="text-align: center;">
          <a-button shape="circle" size="large" style="margin: 0 8px;">
            <span style="font-size: 18px;">🗂️</span>
          </a-button>
          <a-button shape="circle" size="large" style="margin: 0 8px;">
            <span style="font-size: 18px;">📱</span>
          </a-button>
          <a-button shape="circle" size="large" style="margin: 0 8px;">
            <span style="font-size: 18px;">💬</span>
          </a-button>
        </div>
      </div>
    </div>

    <div class="btn-group" style="margin-top: 30px;">
      <button class="nav-btn secondary" @click="$router.push('/')">
        返回首页
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { Message } from '@arco-design/web-vue'
import { IconUser, IconLock } from '@arco-design/web-vue/es/icon'

const router = useRouter()
const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

const rules = {
  username: [
    { required: true, message: '请输入用户名' },
    { minLength: 3, maxLength: 20, message: '用户名长度在 3 到 20 个字符' }
  ],
  password: [
    { required: true, message: '请输入密码' },
    { minLength: 6, maxLength: 50, message: '密码长度在 6 到 50 个字符' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 模拟登录请求
    setTimeout(() => {
      // 保存用户信息到Chrome storage
      const userInfo = {
        username: loginForm.username,
        loginTime: new Date().toISOString()
      }
      
      chrome.storage.local.set({ user: userInfo }, () => {
        loading.value = false
        Message.success('登录成功!')
        router.push('/')
      })
    }, 1500)
    
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
</script>

<style scoped>
.form-container {
  background: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.footer-links {
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.social-login {
  margin-top: 20px;
}
</style> 